package ce.com.newbluedemo.my;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import org.json.JSONArray;

import java.util.ArrayList;

import ce.com.cenewbluesdk.CEBC;

import ce.com.cenewbluesdk.entity.k6.K6_MixInfoStruct;
import ce.com.cenewbluesdk.entity.k6.K6_SendAlarmInfoStruct;
import ce.com.cenewbluesdk.entity.k6.K6_SendDevSettingStruct;
import ce.com.cenewbluesdk.entity.k6.K6_SendGoal;
import ce.com.cenewbluesdk.entity.k6.K6_SendUserInfo;
import ce.com.cenewbluesdk.entity.k6.K6_SendWeatherStruct;
import ce.com.cenewbluesdk.entity.k6.K6_Send_Watch_Face_And_Notification_Set;
import ce.com.cenewbluesdk.proxy.BleFactory;
import ce.com.cenewbluesdk.proxy.CEDevK6Proxy;
import ce.com.cenewbluesdk.uitl.Lg;
import ce.com.cenewbluesdk.uitl.TimeUtil;
import ce.com.newbluedemo.K6.ActivitySendGpsArgument;
import ce.com.newbluedemo.R;

/**
 * Created by Administrator on 2016/12/20 0020.
 */

public class TestK6BlueActivity extends MyActivity implements View.OnClickListener {
    private TextView blue_state;
    private CEDevK6Proxy k6Proxy;

    private boolean gskaiguang = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.acitivty_test_k6);


        blue_state = (TextView) findViewById(R.id.blue_state);
        k6Proxy = BleFactory.getInstance().getK6Proxy();
        if (k6Proxy.isConnectOK()) {
            blue_state.setText("蓝牙连接成功");
        } else {
            blue_state.setText("蓝牙连接失败");
        }


        findViewById(R.id.blue_disconnect).setOnClickListener(this);
        findViewById(R.id.send_time).setOnClickListener(this);
        findViewById(R.id.send_alarm).setOnClickListener(this);
        findViewById(R.id.get_devinfo).setOnClickListener(this);
        findViewById(R.id.send_userinfo).setOnClickListener(this);
        findViewById(R.id.get_max).setOnClickListener(this);
        findViewById(R.id.send_mix).setOnClickListener(this);
        findViewById(R.id.send_setting).setOnClickListener(this);
        findViewById(R.id.send_w).setOnClickListener(this);
        findViewById(R.id.send_m).setOnClickListener(this);
        findViewById(R.id.send_gs).setOnClickListener(this);
        findViewById(R.id.send_pd).setOnClickListener(this);
        findViewById(R.id.send_goal).setOnClickListener(this);
        findViewById(R.id.send_face).setOnClickListener(this);
        findViewById(R.id.send_card).setOnClickListener(this);
        findViewById(R.id.send_contant).setOnClickListener(this);
        findViewById(R.id.start_ota).setOnClickListener(this);
        findViewById(R.id.start_send_gps_file).setOnClickListener(this);


    }

    @Override
    public void receiveData(Message message) {
        if (message.what == CEBC.BLUE_CONNECT_STATE_CHANGE) {
            String s = "";
            if (message.arg1 == CEBC.BLUE_CONNECTED) {
                s = "蓝牙连接成功";
            } else if (message.arg1 == CEBC.BLUE_CONNECTING) {
                s = "蓝牙正在连接";
            } else if (message.arg1 == CEBC.BLUE_DISCONNECT) {
                s = "蓝牙连接失败";
            }
            blue_state.setText(s);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.blue_disconnect:
                k6Proxy.disConnect();
                break;
            case R.id.send_time:
                k6Proxy.getSendHelper().sendTime();
                break;
            case R.id.get_max:

                k6Proxy.getSendHelper().getMixInfo();
                break;
            case R.id.get_devinfo:

                k6Proxy.getSendHelper().getDevInfo();
                break;
            case R.id.send_userinfo:

                k6Proxy.getSendHelper().sendUserInfo(new K6_SendUserInfo(88888, 0, 28, 168, 62, 0));
                break;
            case R.id.send_setting:
                k6Proxy.getSendHelper().sendSetting(new K6_SendDevSettingStruct((byte) 0, (byte) 0, (byte) 1,
                        (byte) 1, (byte) 0, (byte) 1));
                break;
            case R.id.send_alarm:
                K6_SendAlarmInfoStruct alarm = new K6_SendAlarmInfoStruct((byte) 1, (byte) 1, (byte) 1, (byte) 1);
                K6_SendAlarmInfoStruct alarm1 = new K6_SendAlarmInfoStruct((byte) 2, (byte) 2, (byte) 2, (byte) 0);
                K6_SendAlarmInfoStruct alarm2 = new K6_SendAlarmInfoStruct((byte) 3, (byte) 3, (byte) 3, (byte) 1);
                K6_SendAlarmInfoStruct alarm3 = new K6_SendAlarmInfoStruct((byte) 4, (byte) 4, (byte) 4, (byte) 0);
                K6_SendAlarmInfoStruct alarm5 = new K6_SendAlarmInfoStruct((byte) 5, (byte) 5, (byte) 3, (byte) 1);
                ArrayList<K6_SendAlarmInfoStruct> k3s = new ArrayList<>();
                k3s.add(alarm);
                k3s.add(alarm1);
                k3s.add(alarm2);
                k3s.add(alarm3);
                k3s.add(alarm5);

                k6Proxy.getSendHelper().sendAlarmInfo(k3s);
                break;

            case R.id.send_m:
                k6Proxy.getSendHelper().
                        sendMessage_notice(TimeUtil.now(),
                                "冯胖子", "就是一个大忽悠，就是一个大忽悠，就是一个大忽悠就是一个大忽悠，就是一个大忽悠，就是一个大忽悠，就是一个大忽悠，就是一个大忽悠", (byte) 2);
                break;
            case R.id.send_w:

                k6Proxy.getSendHelper().sendWeatherInfo(new K6_SendWeatherStruct(
                        (byte) 2, (byte) 1, (byte) 5, (byte) 0, (byte) 6, (byte) 25));
                break;
            case R.id.send_goal:
                k6Proxy.getSendHelper().sendGoal(new K6_SendGoal(108, 208, 888888888, 888));

                break;
            case R.id.send_gs:
                k6Proxy.getSendHelper().setEnableGsDataTrans(gskaiguang);
                gskaiguang = !gskaiguang;

                break;
            case R.id.send_mix:
                ArrayList<K6_MixInfoStruct.Property> properties = new ArrayList<>();
                K6_SendAlarmInfoStruct xxx = new K6_SendAlarmInfoStruct((byte) 1, (byte) 11, (byte) 12, (byte) 1);
                K6_SendAlarmInfoStruct xxx1 = new K6_SendAlarmInfoStruct((byte) 2, (byte) 12, (byte) 13, (byte) 0);
                K6_SendAlarmInfoStruct xxx2 = new K6_SendAlarmInfoStruct((byte) 3, (byte) 13, (byte) 14, (byte) 1);
//                Send_Alarm_Info_Struct_K3  alarm3=new Send_Alarm_Info_Struct_K3((byte)4,(byte)14,(byte)16,(byte)0);
//                Send_Alarm_Info_Struct_K3  alarm5=new Send_Alarm_Info_Struct_K3((byte)5,(byte)16,(byte)17,(byte)1);
                ArrayList<K6_SendAlarmInfoStruct> xx2 = new ArrayList<>();
                xx2.add(xxx1);
                xx2.add(xxx2);
                xx2.add(xxx);
//                k3s.add(alarm3);
//                k3s.add(alarm5);
                byte[] payload = new byte[K6_SendAlarmInfoStruct.getItemSize() * xx2.size() + 1];
                payload[0] = (byte) xx2.size();
                for (int i = 0; i < xx2.size(); ++i) {
                    K6_SendAlarmInfoStruct this_alrm = xx2.get(i);
                    byte[] this_alarm_payload = this_alrm.getBytes();
                    System.arraycopy(this_alarm_payload, 0, payload, i * K6_SendAlarmInfoStruct.getItemSize() + 1, K6_SendAlarmInfoStruct.getItemSize());
                }
                K6_MixInfoStruct.Property ar = new K6_MixInfoStruct.Property((byte) CEBC.K6.DATA_TYPE_ALARM, payload);
                properties.add(ar);
                properties.add(new K6_MixInfoStruct.Property((byte) CEBC.K6.DATA_TYPE_USERINFO, (new K6_SendUserInfo(1000, 1, 20, 170, 50, 1)).getBytes()));

                K6_MixInfoStruct mixInfoStruct = new K6_MixInfoStruct(properties);
                k6Proxy.getSendHelper().sendMixInfo(mixInfoStruct);
                break;
            case R.id.send_face:
                JSONArray jsonArray = new JSONArray();
                for (int i = 0; i < 32; i++) {
                    if (gskaiguang) {
                        jsonArray.put(i % 2);
                    } else {
                        jsonArray.put((i + 1) % 2);
                    }
                }

                Lg.e(jsonArray.toString());
                k6Proxy.getSendHelper().sendWatchFace(
                        K6_Send_Watch_Face_And_Notification_Set.createWatchFace(jsonArray));
                break;
            case R.id.send_pd:
                k6Proxy.getSendHelper().sendPairStart();
                gskaiguang = !gskaiguang;
                break;
            case R.id.start_ota:
                k6Proxy.startOta();
                break;
            case R.id.start_send_gps_file:
                startActivity(new Intent(this, ActivitySendGpsArgument.class));
                break;
        }

    }
}
